package com.sjtu.rest;

import com.sjtu.constants.TaskConstants;
import com.sjtu.dao.model.ClientInfo;
import com.sjtu.dao.model.TaskInfo;
import com.sjtu.dao.repository.ClientInfoRepository;
import com.sjtu.dao.repository.TaskInfoRepository;
import com.sjtu.feign.TaskInfoFeign;
import com.sjtu.service.cache.TaskInfoChangeReceiver;
import com.sjtu.util.JpaUtil;
import com.sjtu.web.BaseRest;
import com.sjtu.web.Result;
import com.sjtu.web.form.TaskEnableForm;
import com.sjtu.web.form.TaskInfoForm;
import com.sjtu.web.search.TaskInfoSearch;
import com.sjtu.web.vo.ClientInfoVo;
import com.sjtu.web.vo.TaskInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务信息相关
 * nerine
 * 1/29/2020
 */
@RequestMapping(TaskInfoFeign.PREFIX)
@RestController
public class TaskInfoRest extends BaseRest {
    @Autowired
    private ClientInfoRepository clientInfoRepository;
    @Autowired
    private TaskInfoRepository taskInfoRepository;
    @Autowired
    private TaskInfoChangeReceiver taskInfoChangeReceiver;

    @PostMapping("/list")
    public Result<?> list(@RequestBody TaskInfoSearch search) {
        ClientInfo clientInfo = null;
        if(StringUtils.isNotBlank(search.getSystemId())) {
            clientInfo = new ClientInfo();
            clientInfo.setSystemId(search.getSystemId());
            clientInfo = clientInfoRepository.findOne(Example.of(clientInfo)).get();
        }

        TaskInfo query = new TaskInfo();
        query.setClientInfo(clientInfo);
        if(StringUtils.isNotBlank(search.getTaskName())) {
            query.setTaskName(search.getTaskName());
        }

        Page<TaskInfo> result = taskInfoRepository.findAll(Example.of(query),
                PageRequest.of(search.getCurrent(), search.getSize(),
                        Sort.by(Sort.Direction.DESC, "createdDate")));
        List<TaskInfoVo> taskInfoVoList = result.stream().map(TaskInfoRest::createTaskInfoVo).collect(Collectors.toList());
        return success(taskInfoVoList);
    }

    static TaskInfoVo createTaskInfoVo(TaskInfo taskInfo) {
        TaskInfoVo vo = new TaskInfoVo();
        vo.setIdTaskInfo(taskInfo.getIdTaskInfo());
        vo.setTaskDesc(taskInfo.getTaskDesc());
        vo.setTaskName(taskInfo.getTaskName());
        vo.setBeanName(taskInfo.getBeanName());
        vo.setCron(taskInfo.getCron());
        vo.setIsEnable(taskInfo.getIsEnable());
        vo.setTaskType(taskInfo.getTaskType());
        vo.setClientInfo(createClientInfoVo(taskInfo));
        vo.setCreatedDate(taskInfo.getCreatedDate());
        vo.setCreatedBy(taskInfo.getCreatedBy());
        return vo;
    }

    private static ClientInfoVo createClientInfoVo(TaskInfo taskInfo) {
        ClientInfoVo clientInfoVo = new ClientInfoVo();
        if(taskInfo.getClientInfo() != null) {
            clientInfoVo.setIdClientInfo(taskInfo.getClientInfo().getIdClientInfo());
            clientInfoVo.setSystemId(taskInfo.getClientInfo().getSystemId());
            clientInfoVo.setSystemName(taskInfo.getClientInfo().getSystemName());
            clientInfoVo.setInstances(taskInfo.getClientInfo().getInstances());
        }
        return clientInfoVo;
    }

    @GetMapping("/detail/{id}")
    public Result<TaskInfoVo> detail(@PathVariable String id) {
        TaskInfo taskInfo = taskInfoRepository.findById(id).get();
        return success(createTaskInfoVo(taskInfo));
    }

    @PostMapping("/save")
    public Result<?> save(@RequestBody TaskInfoForm form) {
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setSystemId(form.getSystemId());
        clientInfo = clientInfoRepository.findOne(Example.of(clientInfo)).get();

        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setIsEnable(TaskConstants.IsEnable.NO);
        if(form.getIdTaskInfo() != null) {
            taskInfo = taskInfoRepository.findById(form.getIdTaskInfo()).get();
        }
        taskInfo.setIdTaskInfo(form.getIdTaskInfo());
        taskInfo.setTaskName(form.getTaskName());
        taskInfo.setTaskDesc(form.getTaskDesc());
        taskInfo.setBeanName(form.getBeanName());
        taskInfo.setTaskType(form.getTaskType());
        taskInfo.setCron(form.getCron());
        taskInfo.setClientInfo(clientInfo);
        taskInfo.setLeastInterval(form.getLeastInterval());

        JpaUtil.setModifyInfo(form.getOperatorId(), taskInfo);

        String id = taskInfoRepository.save(taskInfo).getIdTaskInfo();
        if(form.getIdTaskInfo() != null) {
            taskInfoChangeReceiver.pushTaskUpdateNotification(form.getIdTaskInfo(), taskInfo.getIsEnable());
        }
        return success(id);
    }

    @PostMapping("/enable")
    public Result<?> enable(@RequestBody TaskEnableForm form) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setIdTaskInfo(form.getIdTaskInfo());
        taskInfo = taskInfoRepository.findOne(Example.of(taskInfo)).get();
        taskInfo.setIsEnable(form.getIsEnable());

        JpaUtil.setModifyInfo(form.getOperatorId(), taskInfo);

        String id = taskInfoRepository.save(taskInfo).getIdTaskInfo();
        taskInfoChangeReceiver.pushTaskUpdateNotification(form.getIdTaskInfo(), taskInfo.getIsEnable());
        return success(id);
    }
}
